package com.kylin.module.community.orm;

import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.module.user.orm.User;

import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.kylin.common.exception.BusinessException;
import com.kylin.common.exception.SystemException;

/**
 * Data access object (DAO) for domain model class Testcompany.
 * @see com.kylin.module.test.orm.Testcompany
 * @author liangzh
 */
public class SitemsgDAO extends BaseHibernateDAO {

    private static final Logger log = Logger.getLogger(SitemsgDAO.class);

 
    
    public List find(User user) {
        log.info("finding msg of " + user.getUsername() );
        try {
            String queryString = "from Sitemsg as f where  f.active=1 and f.user2 = ? order by f.createdon desc";
            Query query = getSession().createQuery(queryString);
            query.setEntity(0, user);
            query.setFirstResult(0);
            query.setMaxResults(10);
   		 //query.setParameter(0, value);
            log.info("size: "+ query.list().size());
            log.info("sql: "+ query.getQueryString());
   		 return query.list();
        }
        catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
           
        }
  	}    

    public Integer findNewById(User user) {
        log.info("finding msg of " + user.getUsername() );
        try {
            String queryString = "select count(*) from Sitemsg as f where  f.active=1 and f.user2 = ?";
            Query query = getSession().createQuery(queryString);
            query.setEntity(0, user);
         // query.setFirstResult(0);
         // query.setMaxResults(10);
   		 // query.setParameter(0, value);
            log.info("size: "+ query.list().size());
            log.info("sql: "+ query.getQueryString());
            if (query.list() != null){
   		 		return new Integer(query.list().get(0).toString());
            }else{
            	return null;
            }
        }
        catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
           
        }
  	}      
    
    public void save(User user, User user2, String message){
 	   log.info("adding ");
 	   Sitemsg flist = new Sitemsg();
 	   
 	   flist.setUser(user);
 	   flist.setUser2(user2);
 	   flist.setMessage(message);
 	   flist.setCreatedon(new Date());
 	   flist.setActive(new Short("1"));
 	   flist.setHits(new Integer(0));	   
 	   
 	   try{
 		   super.save(flist);
 	   }
 	   catch(BusinessException be){
 		   log.info("adding friend"+be.toString());   
 	   }
    }    

    public void read(User user, User user2, String message){
  	   log.info("read update ");
  	   Sitemsg flist = new Sitemsg();
  	   
  	   flist.setUser(user);
  	   flist.setUser2(user2);
  	   flist.setMessage(message);
  	   flist.setCreatedon(new Date());
  	   flist.setActive(new Short("2"));// read
  	   flist.setHits(new Integer(0));	   
  	   
  	   try{
  		   super.save(flist);
  	   }
  	   catch(BusinessException be){
  		   log.info("adding friend"+be.toString());   
  	   }
     }        
    
	public void delete(Sitemsg persistentInstance) {
        log.debug("deleting Sitemsg instance");
        try{
	        super.delete(persistentInstance);
	    }
	    catch (BusinessException be){
	    }

    }
	
	public Integer getTotal(User user){
        try {
            String queryString = "select count(*) from Sitemsg  where   active>=1 and user2 = ?";        	   
            Query query = getSession().createQuery(queryString);
   		 	query.setEntity(0, user);
            log.info("msg count: "+ query.list().get(0));
   		 	return new Integer(query.list().get(0).toString());
         } catch (RuntimeException re) {
            log.error("get sitemsg total", re);
            throw re;
         }		
	}
    
	public List getList(User user, Integer limitstart, Integer limit){
        log.info("finding sitemsg " + limitstart + " " + limit );
        try {
           String queryString = "from Sitemsg   where   active>=1 and user2 = ? order by  createdon desc";        	   
           Query query = getSession().createQuery(queryString);
           query.setEntity(0, user);
           query.setFirstResult(limitstart.intValue());
           query.setMaxResults(limit.intValue());
  		 //query.setParameter(0, value);
           log.info("size: "+ query.list().size());
  		 return query.list();
        } catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
        }
		
	}	
    
    public Sitemsg findById( java.lang.Long id) {
        log.debug("getting Sitemsg instance with id: " + id);
        try{
	        return (Sitemsg)findById(Sitemsg.class , id);
	    }
	    catch (BusinessException be){
	    	return null;
	    }
    }
    
    
    public List findByExample(Sitemsg instance) {
        log.debug("finding Sitemsg instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.kylin.module.SITEMSG.orm.Sitemsg")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Sitemsg instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Sitemsg as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

    public Sitemsg merge(Sitemsg detachedInstance) {
        log.debug("merging Sitemsg instance");
        try {
            Sitemsg result = (Sitemsg) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Sitemsg instance) {
        log.debug("attaching dirty Sitemsg instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Sitemsg instance) {
        log.debug("attaching clean Sitemsg instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}